package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.BeautyPeerlessSkillInfo;
import com.yanqu.road.entity.player.BeautySelfSelectPatronsPosition;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBeautyDaoImpl extends TempDataObjectDao<UserBeauty> {

    public UserBeauty getUserBeauty(long userId ,int beautyId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_beauty where userId=? and beautyId =?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setInt(2, beautyId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBeauty data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Integer, UserBeauty> getUserBeautyMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserBeauty> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_beauty where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBeauty data = parse(rs);
                dataMap.put(data.getBeautyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Long, Map<Integer, UserBeauty>> getUserBeautyMap(List<Long> userIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, UserBeauty>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_beauty where userId in (" + StringUtils.listToString(userIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBeauty data = parse(rs);
                if (!dataMap.containsKey(data.getUserId())) {
                    dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getBeautyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserBeauty data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_beauty(userId,beautyId,intimacy,glamour,level,identityLv,childCount,totalTrystTimes,totalRewardTimes," +
                "identitySkillLv,isShow,createTime,skill1,skill2,skillExp,travelTimes,bathTimes,skinId,skinIntimacy,skinGlamour,quality," +
                "escortExp,escortSkill1,escortSkill2,skinSkill1,skinSkill2,promotionSkill,peerlessUp," +
                "peerlessSkillMap,genderSwapMark,qualityPromotionId,musicRoomSkill,curioGlamour,curioIntimacy," +
                "selfSelectPatronsMap,promoteSkillList,travelBookAlbumSkill,travelBookIntimacy,travelBookGlamour) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBeautyId());
            call.setInt(i++, data.getIntimacy());
            call.setInt(i++, data.getGlamour());
            call.setInt(i++, data.getLevel());
            call.setInt(i++, data.getIdentityLv());
            call.setInt(i++, data.getChildCount());
            call.setInt(i++, data.getTotalTrystTimes());
            call.setInt(i++, data.getTotalRewardTimes());
            call.setInt(i++, data.getIdentitySkillLv());
            call.setBoolean(i++, data.isShow());
            call.setLong(i++, data.getCreateTime());
            call.setInt(i++, data.getSkill1());
            call.setInt(i++, data.getSkill2());
            call.setLong(i++, data.getSkillExp());
            call.setInt(i++, data.getTravelTimes());
            call.setInt(i++, data.getBathTimes());
            call.setInt(i++, data.getSkinId());
            call.setInt(i++, data.getSkinIntimacy());
            call.setInt(i++, data.getSkinGlamour());
            call.setInt(i++, data.getQuality());
            call.setLong(i++, data.getEscortExp());
            call.setInt(i++, data.getEscortSkill1());
            call.setInt(i++, data.getEscortSkill2());
            call.setInt(i++, data.getSkinSkill1());
            call.setInt(i++, data.getSkinSkill2());
            call.setInt(i++, data.getPromotionSkill());
            call.setInt(i++, data.getPeerlessUp());
            call.setString(i++, JSONObject.toJSONString(data.getPeerlessSkillMap()));
            call.setInt(i++, data.getGenderSwapMark());
            call.setInt(i++, data.getQualityPromotionId());
            call.setString(i++, PropertyHelper.parsePropertyToString(data.getMusicRoomSkill()));
            call.setInt(i++, data.getCurioGlamour());
            call.setInt(i++, data.getCurioIntimacy());
            call.setString(i++, JSONObject.toJSONString(data.getSelfSelectPatronsMap()));
            call.setString(i++, JSONObject.toJSONString(data.getPromoteSkillList()));
            call.setString(i++, JSONObject.toJSONString(data.getAlbumSkillMap()));
            call.setInt(i++, data.getTravelBookIntimacy());
            call.setInt(i++, data.getTravelBookGlamour());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBeauty data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_beauty SET intimacy=?,glamour=?,level=?,skill1=?,skill2=?,identityLv=?,childCount=?,totalTrystTimes=?," +
                "totalRewardTimes=?,identitySkillLv=?,isShow=?,createTime=?,skillExp=?,travelTimes=?,bathTimes=?,skinId=?,skinIntimacy=?," +
                "skinGlamour=?,quality=?,escortExp=?,escortSkill1=?,escortSkill2=?,skinSkill1=?,skinSkill2=?,promotionSkill=?,peerlessUp=?," +
                "peerlessSkillMap=?,genderSwapMark=?,qualityPromotionId=?,musicRoomSkill=?,curioGlamour=?," +
                "curioIntimacy=?,selfSelectPatronsMap=?,promoteSkillList=?,travelBookAlbumSkill=?,travelBookIntimacy=?,travelBookGlamour=? " +
                "where userId=? and beautyId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getIntimacy());
            call.setInt(i++, data.getGlamour());
            call.setInt(i++, data.getLevel());
            call.setInt(i++, data.getSkill1());
            call.setInt(i++, data.getSkill2());
            call.setInt(i++, data.getIdentityLv());
            call.setInt(i++, data.getChildCount());
            call.setInt(i++, data.getTotalTrystTimes());
            call.setInt(i++, data.getTotalRewardTimes());
            call.setInt(i++, data.getIdentitySkillLv());
            call.setBoolean(i++, data.isShow());
            call.setLong(i++, data.getCreateTime());
            call.setLong(i++, data.getSkillExp());
            call.setInt(i++, data.getTravelTimes());
            call.setInt(i++, data.getBathTimes());
            call.setInt(i++, data.getSkinId());
            call.setInt(i++, data.getSkinIntimacy());
            call.setInt(i++, data.getSkinGlamour());
            call.setInt(i++, data.getQuality());
            call.setLong(i++, data.getEscortExp());
            call.setInt(i++, data.getEscortSkill1());
            call.setInt(i++, data.getEscortSkill2());
            call.setInt(i++, data.getSkinSkill1());
            call.setInt(i++, data.getSkinSkill2());
            call.setInt(i++, data.getPromotionSkill());
            call.setInt(i++, data.getPeerlessUp());
            call.setString(i++, JSONObject.toJSONString(data.getPeerlessSkillMap()));
            call.setInt(i++, data.getGenderSwapMark());
            call.setInt(i++, data.getQualityPromotionId());
            call.setString(i++, PropertyHelper.parsePropertyToString(data.getMusicRoomSkill()));
            call.setInt(i++, data.getCurioGlamour());
            call.setInt(i++, data.getCurioIntimacy());
            call.setString(i++, JSONObject.toJSONString(data.getSelfSelectPatronsMap()));
            call.setString(i++, JSONObject.toJSONString(data.getPromoteSkillList()));
            call.setString(i++, JSONObject.toJSONString(data.getAlbumSkillMap()));
            call.setInt(i++, data.getTravelBookIntimacy());
            call.setInt(i++, data.getTravelBookGlamour());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBeautyId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBeauty parseData(ResultSet rs) throws SQLException {
        UserBeauty data = new UserBeauty();
        data.setUserId(rs.getLong("userId"));
        data.setBeautyId(rs.getInt("beautyId"));
        data.setIntimacy(rs.getInt("intimacy"));
        data.setGlamour(rs.getInt("glamour"));
        data.setLevel(rs.getInt("level"));
        data.setSkill1(rs.getInt("skill1"));
        data.setSkill2(rs.getInt("skill2"));
        data.setSkillExp(rs.getLong("skillExp"));
        data.setIdentityLv(rs.getInt("identityLv"));
        data.setChildCount(rs.getInt("childCount"));
        data.setTotalTrystTimes(rs.getInt("totalTrystTimes"));
        data.setTotalRewardTimes(rs.getInt("totalRewardTimes"));
        data.setIdentitySkillLv(rs.getInt("identitySkillLv"));
        data.setShow(rs.getBoolean("isShow"));
        data.setCreateTime(rs.getInt("createTime"));
        data.setTravelTimes(rs.getInt("travelTimes"));
        data.setBathTimes(rs.getInt("bathTimes"));
        data.setSkinId(rs.getInt("skinId"));
        data.setSkinIntimacy(rs.getInt("skinIntimacy"));
        data.setSkinGlamour(rs.getInt("skinGlamour"));
        data.setQuality(rs.getInt("quality"));
        data.setEscortExp(rs.getLong("escortExp"));
        data.setEscortSkill1(rs.getInt("escortSkill1"));
        data.setEscortSkill2(rs.getInt("escortSkill2"));
        data.setSkinSkill1(rs.getInt("skinSkill1"));
        data.setSkinSkill2(rs.getInt("skinSkill2"));
        data.setPromotionSkill(rs.getInt("promotionSkill"));
        data.setPeerlessUp(rs.getInt("peerlessUp"));
        String peerlessSkillStr = rs.getString("peerlessSkillMap");
        if (!StringUtils.isNullOrEmpty(peerlessSkillStr)) {
            data.setPeerlessSkillMap(JSON.parseObject(peerlessSkillStr, new TypeReference<ConcurrentHashMap<Integer, BeautyPeerlessSkillInfo>>(){}));
        }
        data.setGenderSwapMark(rs.getInt("genderSwapMark"));
        data.setQualityPromotionId(rs.getInt("qualityPromotionId"));
        data.setCurioGlamour(rs.getInt("curioGlamour"));
        data.setCurioIntimacy(rs.getInt("curioIntimacy"));
        String musicRoomSkill = rs.getString("musicRoomSkill");
        if (!StringUtils.isNullOrEmpty(musicRoomSkill)) {
            data.setMusicRoomSkill(PropertyHelper.parseStringToProperty(musicRoomSkill));
        }
        String selfSelectPatronsStr = rs.getString("selfSelectPatronsMap");
        if (!StringUtils.isNullOrEmpty(selfSelectPatronsStr)) {
            data.setSelfSelectPatronsMap(JSON.parseObject(selfSelectPatronsStr, new TypeReference<ConcurrentHashMap<Integer, BeautySelfSelectPatronsPosition>>(){}));
        }
        String promoteSkillList = rs.getString("promoteSkillList");
        if (!StringUtils.isNullOrEmpty(promoteSkillList)) {
            data.setPromoteSkillList(JSON.parseObject(promoteSkillList, new TypeReference<List<Integer>>(){}));
        }
        String travelBookAlbumSkillStr = rs.getString("travelBookAlbumSkill");
        if (!StringUtils.isNullOrEmpty(travelBookAlbumSkillStr)) {
            data.setAlbumSkillMap(JSON.parseObject(travelBookAlbumSkillStr,
                    new TypeReference<ConcurrentHashMap<Integer, Integer>>(){}));
        }
        data.setTravelBookIntimacy(rs.getInt("travelBookIntimacy"));
        data.setTravelBookGlamour(rs.getInt("travelBookGlamour"));
        return data;
    }
}
